package org.zjut.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zjut.db.ResourceDataBase;
import org.zjut.util.CommonUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Rising
 * @date 2017/8/20
 * @des 资源相关操作控制器
 */
@Controller
public class ResourceController {
    @Autowired
    ResourceDataBase resourceDataBase;

    /**
     * 获取资源及相关属性
     * @param request
     * @return
     */
    @RequestMapping(value = "/getResource", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getResource(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));

        }
        return JSON.toJSONString(resourceDataBase.getResource(params));
    }


    /**
     * 添加资源
     * @param request
     * @return
     */
    @RequestMapping(value = "/addResource", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addResource(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));
        }
        try{
            return JSON.toJSONString(resourceDataBase.addResource(params));
        }catch (Exception e){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
    }

    /**
     * 修改资源
     * @param request
     * @return
     */
    @RequestMapping(value = "/editResource", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String editResource(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));
        }

        try{
            return JSON.toJSONString(resourceDataBase.editResource(params));
        }catch (Exception e){
            return JSON.toJSONString(CommonUtil.setErrorInfo("修改失败"));
        }
    }


    @RequestMapping(value = "/delResource", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delResource(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));
            if(params.containsKey("resourceCode")&&!params.get("resourceCode").toString().isEmpty()){
                return JSON.toJSONString(resourceDataBase.delResource(params.getString("resourceCode")));
            }else{
                return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
            }
        }
    }
}
